package net.jlibrary.invoice.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.invoice.action.vo.ElectronicInvoiceKpActionVo;
import net.jlibrary.invoice.service.IElectronicInvoiceKpInfoManager;

@RequestMapping("/electronicInvoiceKp")
@RestController
public class ElectronicInvoiceKpAction {

	@Autowired
	private IElectronicInvoiceKpInfoManager kpInfoManager;

	@RequestMapping(value = "/electronicAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/electronic/electronicInvoiceKpAdminView");
	}

	// 获得开票列表
	@RequestMapping(value = "/electronicInvoiceAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(ElectronicInvoiceKpActionVo electronicInvoice, PaginationParam param) {
		Pagination pagination = kpInfoManager.listsPage(electronicInvoice, param);
		return pagination;
	}

	// 开票详情
	@RequestMapping(value = "/electronicInvoiceAdminJsonDetail", method = { RequestMethod.GET })
	public ElectronicInvoiceKpActionVo detail(ElectronicInvoiceKpActionVo electronicInvoice) {
		electronicInvoice = kpInfoManager.get(electronicInvoice);
		return electronicInvoice;
	}

	// 初始化开票信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonAddFormInit", method = { RequestMethod.GET })
	public ElectronicInvoiceKpActionVo addFormInit() {
		ElectronicInvoiceKpActionVo electronicInvoice = new ElectronicInvoiceKpActionVo();
		return electronicInvoice;
	}

	// 保存开票信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonSave", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") ElectronicInvoiceKpActionVo electronicInvoice, @Token String token,
			BindingResult result) {
		kpInfoManager.save(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 修改开票信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") ElectronicInvoiceKpActionVo electronicInvoice, @Token String token,
			BindingResult result) {
		kpInfoManager.update(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 删除开票信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		ElectronicInvoiceKpActionVo electronicInvoice = new ElectronicInvoiceKpActionVo();
		electronicInvoice.setId(id);
		kpInfoManager.delete(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 恢复开票信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		ElectronicInvoiceKpActionVo electronicInvoice = new ElectronicInvoiceKpActionVo();
		electronicInvoice.setId(id);
		kpInfoManager.renew(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

}
